package com.lupengning.es.test.doc;

import com.alibaba.fastjson.JSON;
import com.lupengning.es.domain.User;
import java.io.IOException;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

public class ESTest_Doc_Insert_Batch {

  public static void main(String[] args) throws IOException {
    RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("192.168.56.10", 9200, "http"))
    );

    User user = User.builder().name("张三").sex("男").age(18).build();
    String userJson = JSON.toJSONString(user);

    BulkRequest request = new BulkRequest();
    request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan","age",20,"sex","男"));
    request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi","age",30,"sex","男"));
    request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu1","age",30,"sex","男"));
    request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "wangwu2","age",40,"sex","女"));
    request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "wangwu33","age",40,"sex","男"));
    request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name", "wangwu44","age",50,"sex","女"));
    request.add(new IndexRequest().index("user").id("1007").source(XContentType.JSON, "name", "wangwu555","age",60,"sex","男"));
    BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);

    System.out.println(response.getItems());
    esClient.close();
  }
}
